//
//  CWTabBarController.swift
//  新浪微博练习02
//
//  Created by 曹魏 on 16/9/22.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class CWTabBarController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        //将uitabbarViewController的属性tabbar替换成自定义的tababar,目的是为了添加一个+号
        //        self.tabBar = CWMainTabBar
        //直接赋值会报错,因为tabbar是只读,所以不能进行直接赋值操作,但可以间接赋值,想到了kvc赋值
        //定义一个tabbar
        let hmTabbar = CWMainTabBar()
        //加weak是为了避免循环引用
        hmTabbar.composeClosure = { [weak self] in
            print("撰写按钮被点击",self)
        }
        
        
        self.setValue(hmTabbar, forKey: "tabBar")
        //添加子视图控制器
        addChildViewControllers()
        
        
        
        
        //设置默认的渲染颜色
//        tabBar.tintColor = UIColor.orange
    }
    //添加子视图控制器方法
    private func addChildViewControllers() {
        addChildViewController(vc: CWHomeViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(vc: CWMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(vc: CWDisableTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(vc: CWProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    private func addChildViewController(vc: UIViewController,title: String,imageName: String) {
        //实例化第一个一个tableviewController
//        let home = CWHomeViewController()
        //设置上面的title
        vc.navigationItem.title = title
        //设置下面的title和image
        vc.tabBarItem.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        //设置选中按钮的颜色
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.withRenderingMode(UIImageRenderingMode.alwaysOriginal)
        //设置选中按钮下面文字的颜色
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orange], for: .selected)
        //修改文字的大小
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 11)], for: .normal)
        //修改徽标的颜色和值
        vc.tabBarItem.badgeColor = UIColor.red
        //设置偏移
        vc.tabBarItem.titlePositionAdjustment = UIOffset(horizontal: 0, vertical: -2)
//        vc.tabBarItem.badgeValue = "8000"//牛逼
        vc.tabBarItem.badgeValue = nil
        
        
        
        
        
        
        let nav = CWBaseNavController(rootViewController: vc)
        addChildViewController(nav)
    }
}

















